import md5 from "md5";
import CryptoJS from "crypto-js";
// const {
// 	CryptoJSLocal
// } = require('./aes')
//秘钥,一串十六进制的数据，每两个算一组
var prekey = "6c204b2eab9d3433";
var key = "";
var caculation = (mac, rnd, pre_s_eed) => {
  // todo
  key = prekey;
};

var setS0 = (sn) => {
  key = md5(strToHexCharCode(sn));
  return key;
};

var asciiStrToHexCharCode = (str) => {
  if (str === "") return "";
  var hexCharCode = [];
  for (var i = 0; i < str.length; i++) {
    hexCharCode.push(str.charCodeAt(i).toString(16));
  }
  return hexCharCode.join("");
};

var strToHexCharCode = (str) => {
  var pos = 0;
  var len = str.length;
  if (len % 2 != 0) {
    return null;
  }
  len /= 2;
  var arrBytes = new Array();
  for (var i = 0; i < len; i++) {
    var s = str.substr(pos, 2);
    var v = parseInt(s, 16);
    arrBytes.push(v);
    pos += 2;
  }
  return arrBytes;
};

var hexCharCodeToStr = (hexCharCodeStr) => {
  var trimedStr = hexCharCodeStr.trim();
  var rawStr =
    trimedStr.substr(0, 2).toLowerCase() === "0x"
      ? trimedStr.substr(2)
      : trimedStr;
  var len = rawStr.length;
  if (len % 2 !== 0) {
    return "";
  }
  var curCharCode;
  var resultStr = [];
  for (var i = 0; i < len; i = i + 2) {
    curCharCode = parseInt(rawStr.substr(i, 2), 16);
    resultStr.push(String.fromCharCode(curCharCode));
  }
  return resultStr.join("");
};

//aes ecb 128 加密
var aesEncrypt = (code) => {
  var key = CryptoJS.enc.Utf8.parse(prekey);
  var encryptedData = CryptoJS.AES.encrypt(code, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.ZeroPadding,
  });
  encryptedData = encryptedData.ciphertext.toString(CryptoJS.enc.Hex);
  return encryptedData;
};

//aes ecb 128 解密
var aesDecrypt = (encryptedData) => {
  var key = CryptoJS.enc.Utf8.parse(prekey);
  var encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedData);
  var encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  var decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.ZeroPadding,
  });
  var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
  return decryptedStr;
};
//数据封装
var hexBuffer = (str) => {
  //字符串不存在，返回一个数组长度为16的0
  if (!str)
    return [...Array(16)].map((_, i) => {
      return 0;
    });
  //将字符串每两个为一组都转化为10进制的数据，数组长度为16，不够的填0
  return [...Array(16)].map((_, i) => {
    return parseInt(str.substr(2 * i, 2), 16) || 0;
  });
};
//数据解析
var ab2hex = (buffer) => {
  return Array.prototype.map
    .call(new Uint8Array(buffer), (x) => ("00" + x.toString(16)).slice(-2))
    .join("");
};

// 字符串转为ArrayBuffer对象，参数为字符串
var str2ab = (str) => {
  // 将16进制转化为ArrayBuffer
  return new Uint8Array(
    str.match(/[\da-f]{2}/gi).map(function (h) {
      return parseInt(h, 16);
    })
  ).buffer;
};

var hex_to_ascii = (str1) => {
  var hex = str1.toString();
  var str = "";
  for (var n = 0; n < hex.length; n += 2) {
    str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
  }
  return str;
};

var ascii_to_hex = (str) => {
  var arr1 = [];
  for (var n = 0, l = str.length; n < l; n++) {
    var hex = Number(str.charCodeAt(n)).toString(16);
    arr1.push(hex);
  }
  return arr1.join("");
};
var hex2int = (hex) => {
  var len = hex.length,
    a = new Array(len),
    code;
  for (var i = 0; i < len; i++) {
    code = hex.charCodeAt(i);
    if (48 <= code && code < 58) {
      code -= 48;
    } else {
      code = (code & 0xdf) - 65 + 10;
    }
    a[i] = code;
  }

  return a.reduce(function (acc, c) {
    acc = 16 * acc + c;
    return acc;
  }, 0);
};
var autoAddZero = (val, len) => {
  val = val.toString(16);
  var cache = "";
  for (var i = 0; i < len - val.length; i++) {
    cache += 0;
  }
  return cache + val;
};

export default {
  caculation,
  setS0,
  aesEncrypt,
  aesDecrypt,
  asciiStrToHexCharCode,
  strToHexCharCode,
  hexCharCodeToStr,
  hexBuffer,
  ab2hex,
  str2ab,
  hex_to_ascii,
  ascii_to_hex,
  hex2int,
  autoAddZero,
};
